﻿using System;
using System.Diagnostics;
using Microsoft.AspNetCore.Http;//session
using Microsoft.AspNetCore.Mvc;
using 医院预约Web.Models;

namespace 医院预约Web.Controllers
{
    public class HomeController : Controller
    {
        Models.Account_password ap = new Models.Account_password();
        public IActionResult Index()
        {
            return View();
        }

        public IActionResult About()
        {
            ViewData["Message"] = "Your application description page.";

            return View();
        }

        public IActionResult Contact()
        {
            ViewData["Message"] = "Your contact page.";

            return View();
        }

        public IActionResult Privacy()
        {
            return View();
        }

        public IActionResult DefaultPage()
        {
            string account = HttpContext.Session.GetString("name");
            ViewData["id"] = account;
            return View();
        }
        public IActionResult dbShow()
        {
            DataBase context = HttpContext.RequestServices.GetService(typeof(DataBase)) as DataBase;
            return View(context.GetAllAccount());
        }
        // 下面 Login()重载为[HttpGet]请求，[HttpGet]是默认请求
        public IActionResult Login()
        {
            return View();
        }

        [HttpPost]  //指定下Login方法重载为HttpPost请求，Login视图中，from表单请求方式：method="post"
        public void Login(string uname = "", string userpwd = "")//
        {
            DataBase db = HttpContext.RequestServices.GetService(typeof(DataBase)) as DataBase;
            if (db.isaccountright(uname, userpwd) == true)//Models中的方法判定
            {
                switch (db.getviplevel(uname))
                {
                    case "0":
                        HttpContext.Session.SetString("name", uname);
                        Response.Redirect(Url.Action("DefaultPage", "Home"));
                        break;
                    case "1":
                        HttpContext.Session.SetString("name", uname);//登陆姓名存入Session
                        Response.Redirect(Url.Action("ba2d0cf27b323a48", "Vip"));
                        break;
                    case "2"://Doctor
                        HttpContext.Session.SetString("name", uname);
                        Response.Redirect(Url.Action("Index", "Doctor"));
                        break;
                    case "3"://Admin
                        HttpContext.Session.SetString("name", uname);
                        Response.Redirect(Url.Action("Index", "Admin"));
                        break;
                    case "9":
                        HttpContext.Session.SetString("name", uname);
                        Response.Redirect(Url.Action("ae99fee77bffd211851b7d", "Vip"));
                        break;
                }
            }
            else Response.Redirect("/Admin/dialog?id=8");

        }
        public void LoginB()//
        {
            HttpContext.Session.SetString("name", "hacker");
            Response.Redirect(Url.Action("DefaultPage", "Home"));
        }
        public void LoginV()//
        {
            HttpContext.Session.SetString("name", "user");//登陆姓名存入Session
            Response.Redirect(Url.Action("ba2d0cf27b323a48", "Vip"));
        }
        public void LoginD()//
        {
            HttpContext.Session.SetString("name", "1001");
            Response.Redirect(Url.Action("Index", "Doctor"));
        }
        public IActionResult register()
        {
            return View();
        }
        [HttpPost]
        public void register(string uname = "", string userpwd = "", string message = "")
        {

            DataBase db = HttpContext.RequestServices.GetService(typeof(DataBase)) as DataBase;
            System.DateTime datetime = new System.DateTime();
            datetime = System.DateTime.Now;//yyyy-MM-dd HH:mm:ss
            if (db.dbregister(uname, userpwd, datetime, message)) Response.Redirect(Url.Action("Login", "Home"));
            else Response.Redirect("/Admin/dialog?id=9");
        }
        public IActionResult LoginError()
        {
            return View();
        }

        [ResponseCache(Duration = 0, Location = ResponseCacheLocation.None, NoStore = true)]
        public IActionResult Error()
        {
            return View(new ErrorViewModel { RequestId = Activity.Current?.Id ?? HttpContext.TraceIdentifier });
        }
        public void nofreeze(string id)
        {
            DataBase db = HttpContext.RequestServices.GetService(typeof(DataBase)) as DataBase;
            if (db.nofreezemessage(id)) Response.Redirect(Url.Action("Login", "Home"));
            else Response.Redirect(Url.Action("LoginError", "Home"));
        }

    }
}
